<!DOCTYPE html>
<html>

<head>
    <title>Preview Display</title>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2"/>
    <style>
        html,
        body {
            padding: 0px;
            margin: 0px;
        }
    </style>
    <script src="../libs/jquery-3.4.1.min.js"></script>
    <script src='../libs/message.js'></script>
    <script src='../custom/configs/htconfig.js'></script>
    <script src='../custom/libs/echarts.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src='../libs/plugin/ht-obj.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>
    <script src='../libs/plugin/ht-cssanimation.js'></script>
    <script src="../libs/plugin/ht-autolayout.js"></script>
    <script src='../libs/plugin/ht-dialog.js'></script>
    <script src='../conf/confURL.js'></script>
    <script src='../conf/workshopAjax.js'></script>
    <script>
        dataModel = new ht.DataModel();
        graphView = new ht.graph.GraphView(dataModel);
        dataModel.a('layout_editorURL', layout_editorURL);
        // 车间编码
        // let workshopCode = 'HT_SC_CJ_XD1112_XD111203';
        let workshopCode = GetQueryString('workshopCode');
        workshopCode = workshopCode.substring(workshopCode.lastIndexOf("_") + 1, workshopCode.length);
        let kanban = '';
        let kanbanTitle = '';
        let factoryStatusTable = '';
        let factoryStatusTableZj = '';
        let factoryStatusTitle = '';
        let processQualifiedQuantity = '';
        let processQualifiedQuantityTitle = '';
        let productsQualifiedQuantity = '';
        let productsQualifiedQuantityTitile = '';

        let frame = document.createElement('iframe');
        var dialog = window.dialog = new ht.widget.Dialog();

        function init() {
            graphView.addToDOM();
            ht.Default.xhrLoad('displays/智慧兴达/兴达八厂/生产监控/workshop_table.json', function (text) {
                var json = ht.Default.parse(text);
                if (json.title) document.title = json.title;
                dataModel.deserialize(json);
                graphView.fitContent(true);
                initParams();
                fiveMinutes();
                setInterval(fiveMinutes, 1000 * 60 * 5);
                // 机床状态信息 与 车间报警灯 请求频率一致 20s; 生产看板: 20s
                setInterval(setIntervalTime, 1000 * 20);
                setConfig(frame, dialog);
                openInterval();
            });

        }

        // 报表弹出框
        function showDialog(url) {
            setFrameStyle(frame, dialog, url);
        }

        function setIntervalTime(){
            //看板
            getProductionIndexBoard(workshopCode);
            // 机床状态信息
            getBedCodeStatus(workshopCode);
        }

        function fiveMinutes() {
            // 工序 5分钟
            getProcessQualifiedQuantity(workshopCode);
            // 规格合格量 5分钟
            getProductsQualifiedQuantity(workshopCode);
        }

        function initParams() {

            kanban = dataModel.getDataByTag('kanban');
            kanbanTitle = dataModel.getDataByTag('kanbanTitle');

            factoryStatusTable = dataModel.getDataByTag('factoryStatusTable');
            factoryStatusTableZj = dataModel.getDataByTag('factoryStatusTable1');
            factoryStatusTitle = dataModel.getDataByTag('factoryStatusTitle');

            processQualifiedQuantity = dataModel.getDataByTag('processQualifiedQuantity');
            processQualifiedQuantityTitle = dataModel.getDataByTag('processQualifiedQuantityTitle');

            productsQualifiedQuantity = dataModel.getDataByTag('productsQualifiedQuantity');
            productsQualifiedQuantityTitile = dataModel.getDataByTag('productsQualifiedQuantityTitile');
        }

        // 右下角柱状图 坐标轴点击事件
        window.notifier = new ht.Notifier();
        notifier.add(function (e) {
            if (e.type === 'clickXAxis') {
                var div = e.dom; // 这里就拿到 dom 了
                div.on('click', function (params) {
                    // 点击横坐标轴
                    if ("xAxis" === params.componentType) {
                        // 给父页面workshop.html传值
                        window.top.postMessage(params.value);
                        // 点击柱子
                    } else if ("series" === params.componentType) {
                        // 给父页面workshop.html传值
                        window.top.postMessage(params.name);
                    }
                });
            }
        });


        // 车间报表无需权限控制，进入该页面的都可见
        // 左一
        function productionIndexBoard(prodectionData) {
            if (prodectionData.title != null && prodectionData.title != undefined) {
                kanbanTitle.s('text', prodectionData.title);
            }

            if (prodectionData.data != null && prodectionData.data != undefined && prodectionData.data != '') {
                kanban.a('ht.dataSource', prodectionData.data);
            }
        }

        // 左二 机床状态信息
        function bedCodeStatus(bedCodeData) {
            if (bedCodeData.title != null && bedCodeData.title != undefined && bedCodeData.title != '') {
                factoryStatusTitle.s('text', bedCodeData.title);
            }

            if (bedCodeData.data != null && bedCodeData.data != undefined && bedCodeData.data != '') {
                factoryStatusTable.a('ht.dataSource', bedCodeData.data);
            }

            if (bedCodeData.total != null && bedCodeData.total != undefined && bedCodeData.total != '') {
                factoryStatusTableZj.a('ht.dataSource', bedCodeData.total);
            }
        }

        // 右一
        function processQualified(processData) {
            if (processData.title != null && processData.title != undefined) {
                processQualifiedQuantityTitle.s('text', processData.title);
            }

            if (processData.data != null && processData.data != undefined && processData.data != '') {
                processQualifiedQuantity.a('xAxis', Boolean(processData.data.month) ? processData.data.month : '');
                processQualifiedQuantity.a('yAxis1', Boolean(processData.data.produceNum) ? processData.data.produceNum : 0);
                processQualifiedQuantity.a('yAxis2', Boolean(processData.data.qualifiedNum) ? processData.data.qualifiedNum : 0);
                processQualifiedQuantity.a('yAxis3', Boolean(processData.data.qualifiedRate) ? processData.data.qualifiedRate : 0);
            }
        }

        // 右二
        function productsQualified(qualifiedData) {
            if (qualifiedData.title != null && qualifiedData.title != undefined) {
                productsQualifiedQuantityTitile.s('text', qualifiedData.title);
            }

            if (qualifiedData.data != null && qualifiedData.data != undefined && qualifiedData.data != '') {
                productsQualifiedQuantity.a('xdata', Boolean(qualifiedData.data.produce) ? qualifiedData.data.produce : '');
                productsQualifiedQuantity.a('data1', Boolean(qualifiedData.data.yield) ? qualifiedData.data.yield : 0);
                productsQualifiedQuantity.a('data2', Boolean(qualifiedData.data.qualified) ? qualifiedData.data.qualified : 0);
            }
        }


        //表格自动滚动
        function tableRun() {
            let tabledata = factoryStatusTable.a("ht.dataSource");
            if (tabledata != undefined && tabledata.length > 6) {
                var old = factoryStatusTable.a('ht.translateY');
                factoryStatusTable.a('ht.translateY', old - 44);

                tabledata.push(tabledata[0]);
                tabledata.splice(0, 1);

                factoryStatusTable.a("ht.dataSource", tabledata);
                factoryStatusTable.a('ht.translateY', old);
            }
        }

        //重新打开定时器，播放滚动
        function openInterval() {
            tableTimer = setInterval(tableRun, 2000);
        }
    </script>
</head>

<body onload='setTimeout(init, 300)'>
</body>

</html>